package com.example.neusoft.yuejian_12.Activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.neusoft.yuejian_12.Fragment.ClubFragment;
import com.example.neusoft.yuejian_12.Fragment.HomeFragment;
import com.example.neusoft.yuejian_12.Fragment.PersonFragment;
import com.example.neusoft.yuejian_12.Fragment.PhotoFragment;
import com.example.neusoft.yuejian_12.Fragment.TrainingFragment;
import com.example.neusoft.yuejian_12.R;



public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private LinearLayout mTabClub;
    private LinearLayout mTabTraining;
    private LinearLayout mTabHome;
    private LinearLayout mTabPhoto;
    private LinearLayout mTabPersonal;

    private ImageButton mImgClub;
    private ImageButton mImgTraining;
    private ImageButton mImgHome;
    private ImageButton mImgPhoto;
    private ImageButton mImgPersonal;

    private android.support.v4.app.Fragment mTab01;
   private android.support.v4.app.Fragment mTab02;
   private android.support.v4.app.Fragment mTab03;
    private android.support.v4.app.Fragment mTab04;
    private android.support.v4.app.Fragment mTab05;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(0);
    }

    private void initEvent() {
        mTabClub.setOnClickListener(this);
        mTabTraining.setOnClickListener(this);
        mTabHome.setOnClickListener(this);
        mTabPhoto.setOnClickListener(this);
        mTabPersonal.setOnClickListener(this);
    }

    private void initView() {
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_weixin);
        mTabTraining = (LinearLayout) findViewById(R.id.id_tab_training);
        mTabHome = (LinearLayout) findViewById(R.id.id_tab_frd);
        mTabPhoto = (LinearLayout) findViewById(R.id.id_tab_photo);
        mTabPersonal = (LinearLayout) findViewById(R.id.id_tab_person);

        mImgClub = (ImageButton) findViewById(R.id.id_tab_weixin_img);
        mImgTraining = (ImageButton) findViewById(R.id.id_tab_training_img);
        mImgHome = (ImageButton) findViewById(R.id.id_tab_frd_img);
        mImgPhoto = (ImageButton) findViewById(R.id.id_tab_photo_img);
        mImgPersonal = (ImageButton) findViewById(R.id.id_tab_person_img);
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {
            case 0:
               if (mTab01 == null) {
                   mTab01 = new ClubFragment();
                    transaction.add(R.id.id_content, mTab01);
                } else {
                    transaction.show(mTab01);
               }
               mImgClub.setImageResource(R.drawable.ic_club_y);
                break;
            case 1:
               if (mTab02 == null) {
                    mTab02 = new TrainingFragment();transaction.add(R.id.id_content, mTab02);
               } else {
                    transaction.show(mTab02);
               }
                mImgTraining.setImageResource(R.drawable.ic_training_y);
                break;
            case 2:
                if (mTab03 == null) {
                    mTab03 = new HomeFragment();
                   transaction.add(R.id.id_content, mTab03);
                } else {
                   transaction.show(mTab03);
                }
              mImgHome.setImageResource(R.drawable.ic_lianmeng_y);
              break;
            case 3:
                if (mTab04 == null) {
                    mTab04 = new PhotoFragment();
                    transaction.add(R.id.id_content, mTab04);
                } else {
                    transaction.show(mTab04);
                }
                mImgPhoto.setImageResource(R.drawable.ic_photo_y);
                break;
            case 4:
                if (mTab05 == null) {
                    mTab05 = new PersonFragment();
                    transaction.add(R.id.id_content, mTab05);
                } else {
                    transaction.show(mTab05);
                }
                mImgPersonal.setImageResource(R.drawable.ic_user_y);
                break;

            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mTab01 != null) {
           transaction.hide(mTab01);
        }
        if (mTab02 != null) {
            transaction.hide(mTab02);
        }
       if (mTab03 != null) {
            transaction.hide(mTab03);
        }
        if (mTab04 != null) {
            transaction.hide(mTab04);
        }
        if (mTab05 != null) {
            transaction.hide(mTab05);
        }
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId()) {
            case R.id.id_tab_weixin:
              setSelect(0);
             break;
            case R.id.id_tab_training:
                setSelect(1);
                break;
            case R.id.id_tab_frd:
              setSelect(2);
               break;
            case R.id.id_tab_photo:
                setSelect(3);
                break;
            case R.id.id_tab_person:
                setSelect(4);
                break;

            default:
                break;
        }
    }

    private void resetImgs() {
        mImgClub.setImageResource(R.drawable.ic_club_w);
        mImgTraining.setImageResource(R.drawable.ic_training_w);
        mImgHome.setImageResource(R.drawable.ic_lianmeng_w);
        mImgPhoto.setImageResource(R.drawable.ic_photo_w);
        mImgPersonal.setImageResource(R.drawable.ic_user_w);
    }

}
